var express = require('express');
var apiCommon = express.Router();
var Mock = require('mockjs');
var _center = {
    'centerId': '@id()',
    'name': '@city(true) @cword(2,4) 中心',
    'openTime': "@time('HH:mm') - @time('HH:mm')",
    'address': "@county(true) 某某路 @integer(0,100) 号",
    'sportType': "@string('篮足羽铅',1)球",
    "phone": "400888888",
    "description": "@cparagraph",
    'pic|8': ['@image("600x400", "@color", "#fff", "png", "IMAGE")']
}

var _centerdetail = {
    'centerId': '@id()',
    'name': '@city(true) @cword(2,4) 中心',
    'openTime': "@time('HH:mm') - @time('HH:mm')",
    'address': "@county(true) 某某路 @integer(0,100) 号",
    'sportType': "@string('篮足羽铅',1)球",
    "phone": "400888888",
    "description": "",
    "courts": ""
}

var _court = {
    "id": "@increment()",
    "sportType": "@integer(1,6)",
    "maxDays": 7,
    "bookType": [
        1
    ],
    "defaultPrice": "@integer(0,100)",
    'openTime': "@time('HH:mm') - @time('HH:mm')",
}


var _courtDate = {
    "dateRange|7": [{
        "date": "@date('yyyyMMdd')",
        "weekday": "周@cword('一二三四五六日', 1)"
    }]
}


var _courtField = {
    "date": "@date('yyyyMMdd')",
    "fieldName|6": [{
        "index|1-6": 1,
        "name": "@cword(2,4)场地"
    }],
    "timeRange": ["10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00"],
    "availableFields": [
        ['10:00', 1, 1004, 0, 46],
        ['11:00', 2, 2004, 0, 36],
        ['12:00', 3, 3004, 0, 30],
        ['13:00', 4, 4004, 0, 42],
        ['14:00', 5, 5004, 0, 41],
        ['15:00', 3, 6004, 0, 50],
        ['16:00', 2, 7004, 0, 49],
        ['17:00', 1, 8004, 0, 49]
    ]
}


// console.log(Mock.mock(_courtField));

apiCommon.get('/centers', function(request, response, next) {
    var id = request.param('id');
    var data = Mock.mock({ 'array|10': [_center] });
    response.writeHead(200, { 'Content-Type': 'application/json;charset=utf-8' });
    var responseData = data.array;

    setTimeout(() =>
        response.end(JSON.stringify(responseData)), 1000
    )
});



apiCommon.get('/centers/:id', function(request, response, next) {
    var id = request.param('id');

    var data = Mock.mock({ 'center': _center })
    var courts = Mock.mock({ 'courts|3': [_court] });
    data.center.courts = courts.courts;
    response.writeHead(200, { 'Content-Type': 'application/json;charset=utf-8' });
    var responseData = data.center;

    setTimeout(() =>
        response.end(JSON.stringify(responseData)), 1000
    )
});


apiCommon.get('/court/date', function(request, response, next) {
    var courtid = request.param('court');
    console.log("courtid:" + courtid);

    var dateRanage = Mock.mock(_courtDate);
    response.writeHead(200, { 'Content-Type': 'application/json;charset=utf-8' });
    var responseData = dateRanage;

    setTimeout(() =>
        response.end(JSON.stringify(responseData)), 1000
    )
});


apiCommon.get('/court/fields', function(request, response, next) {
    var courtid = request.param('court');
    var date = request.param('date');
    console.log("courtid:" + courtid);
    console.log("date:" + date);
    var courtField = Mock.mock(_courtField);
    response.writeHead(200, { 'Content-Type': 'application/json;charset=utf-8' });
    var responseData = courtField;
    console.log(courtField);
    setTimeout(() =>
        response.end(JSON.stringify(responseData)), 1000
    )
});


module.exports = apiCommon;
